//package service;
//
//import model.User;
//import java.util.HashMap;
//import java.util.Map;
//
//public class AuthService {
//    // 1. 添加final修饰符
//    private final Map<String, User> users;
//    private static AuthService instance;
//
//    private AuthService() {
//        // 2. 初始化users
//        users = new HashMap<>();
//        // 初始化测试用户
//        users.put("user1", new User("user1", "123456", false));
//        users.put("admin1", new User("admin1", "admin123", true));
//    }
//
//    // 添加注册方法
//    public boolean register(String username, String password, boolean isAdmin) {
//        if (users.containsKey(username)) {
//            return false; // 用户名已存在
//        }
//        users.put(username, new User(username, password, isAdmin));
//        return true;
//    }
//
//    // 3. 修正getInstance方法
//    public static synchronized AuthService getInstance() {
//        if (instance == null) {
//            instance = new AuthService();
//        }
//        return instance;
//    }
//
//    // 4. 修正authenticate方法
//    public User authenticate(String username, String password, boolean isAdmin) {
//        User user = users.get(username);
//        // 5. 添加null检查
//        if (user != null && user.getPassword().equals(password)) {  // 修正括号
//            if (user.isAdmin() == isAdmin) {
//                return user;
//            }
//        }
//        return null;
//    }
//}

package service;

import model.User;
import util.DatabaseUtil;
import java.sql.*;

public class AuthService {
    // 单例模式（可选）
    private static AuthService instance;
    public static synchronized AuthService getInstance() {
        if (instance == null) {
            instance = new AuthService();
        }
        return instance;
    }
    public User authenticate(String username, String password, boolean isAdmin) {
        String sql = "SELECT * FROM users WHERE username = ? AND password = ? AND is_admin = ?";

        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, username);
            stmt.setString(2, password);
            stmt.setBoolean(3, isAdmin);

            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                return new User(
                        rs.getString("username"),
                        rs.getString("password"),
                        rs.getBoolean("is_admin")
                );
            }
        } catch (SQLException e) {
            System.err.println("数据库查询错误：" + e.getMessage());
        }
        return null;
    }
    // 新增注册方法（数据库版）
    public boolean register(String username, String password, boolean isAdmin) {
        // 1. 先检查用户名是否已存在
        String checkSql = "SELECT username FROM users WHERE username = ?";

        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement checkStmt = conn.prepareStatement(checkSql)) {

            checkStmt.setString(1, username);
            ResultSet rs = checkStmt.executeQuery();
            if (rs.next()) {
                return false; // 用户名已存在
            }

            // 2. 插入新用户
            String insertSql = "INSERT INTO users (username, password, is_admin) VALUES (?, ?, ?)";
            try (PreparedStatement insertStmt = conn.prepareStatement(insertSql)) {
                insertStmt.setString(1, username);
                insertStmt.setString(2, password);
                insertStmt.setBoolean(3, isAdmin);

                int affectedRows = insertStmt.executeUpdate();
                return affectedRows > 0; // 返回是否插入成功
            }
        } catch (SQLException e) {
            System.err.println("注册错误：" + e.getMessage());
            return false;
        }
    }
}